<template>
  <el-dialog
    title="选择导出部门"
    width="600px"
    :visible.sync="visible"
    append-to-body
    @close="hide"
  >
    <el-row>
      <el-col :span="12">
        <div class="org-tree-select-box">
          <div class="org-tree-select-label">请选择导出部门：</div>
          <div class="org-tree-select-result">
            <search-org-tree ref="searchOrgTree" show-checkbox @on-select-change="onSelectChange" />
          </div>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="org-tree-select-box right-box">
          <div class="org-tree-select-label">已选：</div>
          <div class="org-tree-select-result">
            <div v-for="item in selectList" :key="item.id" class="org-tree-select-result-item">
              {{ item.deptName }}
              <i class="el-icon-delete" @click="deleteItem(item)"></i>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div slot="footer">
      <el-button @click="hide">取 消</el-button>
      <el-button type="primary" @click="ok">确 定</el-button>
      <el-button type="primary" @click="all">导出企业全部员工</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import SearchOrgTree from './SearchOrgTree';

  export default {
    components: { SearchOrgTree },
    data() {
      return {
        visible: false,
        selectList: [],
      };
    },
    methods: {
      show() {
        this.selectList = [];
        this.visible = true;
      },
      hide() {
        this.selectList = [];
        this.$refs['searchOrgTree'].init();
        this.visible = false;
      },
      ok() {
        this.$emit(
          'on-ok',
          this.selectList.map(item => item.id)
        );
        this.hide();
      },
      all() {
        const arr = this.$refs['searchOrgTree'].tranArr;
        this.$emit(
          'on-ok',
          arr.map(item => item.id)
        );
        this.hide();
      },
      onSelectChange(list) {
        this.selectList = list;
        console.log(list);
      },
      deleteItem(item) {
        this.$refs['searchOrgTree'].unselectItem(item);
        // this.selectList = this.selectList.filter(m => item.id !== m.id);
      },
    },
  };
</script>
<style lang="scss" scoped>
  .org-tree-select-box {
    &.right-box {
      margin-left: 10px;
    }
  }
  .org-tree-select-result {
    border: 1px solid #dddddd;
    border-radius: 5px;
    padding: 10px;
    height: 360px;
    overflow: hidden;
  }
  .org-tree-select-label {
    margin-bottom: 10px;
  }
  .org-tree-select-result-item {
    height: 24px;
    line-height: 24px;
    position: relative;
    padding: 0 10px;
    cursor: pointer;
    .el-icon-delete {
      display: none;
      position: absolute;
      right: 10px;
      top: 5px;
    }
    &:hover {
      background-color: #f5f7fa;
      .el-icon-delete {
        display: unset;
      }
    }
  }
</style>
